package com.masterclient.app.businessmodule.usermodule.login.contract;

import com.eoner.common.config.Config;
import com.eoner.baselib.presenter.ThrowableConsumer;
import com.masterclient.app.businessmodule.usermodule.api.UserHttpService;
import com.masterclient.app.httpservice.RetrofitUtil;
import com.masterclient.app.businessmodule.usermodule.login.beans.CaptchaBean;
import com.masterclient.app.businessmodule.usermodule.login.beans.LoginBean;
import com.masterclient.app.businessmodule.usermodule.login.beans.LoginResultBean;
import com.eoner.baselibrary.utils.EncryptUtil;
import com.masterclient.app.utils.CaptchaUtil;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.Observable;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import okhttp3.RequestBody;

/**
* Created by TMVPHelper on 2017/07/24
*/
public class BringPwdBackPresenter extends BringPwdBackContract.Presenter{

    @Override
    public void forgotSetPwd(String captcha, String mobile, String pwd,String type) {

        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("mobile",mobile);
            jsonObject.put("captcha",captcha);
            jsonObject.put("pwd",pwd);
            jsonObject.put("type",type);
            jsonObject.put("invite_code", "");
            jsonObject.put("uid",Config.uid);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),
                jsonObject.toString());
        Observable observable = UserHttpService.getInstance().forgotSetPwd(body);
        Disposable disposable = toSubscribe(observable, new Consumer<LoginResultBean>() {
            @Override
            public void accept(@NonNull LoginResultBean bean) throws Exception {
                if("0".equals(bean.getCode())){
                    mView.setPwdSuccess();
                }else {
                    mView.setPwdFailed(bean.getMsg());
                }
            }
        },new ThrowableConsumer(){
            @Override
            public void accept(Throwable o) throws Exception {
                super.accept(o);
                mView.setPwdFailed("网络异常，请重试");
            }
        });
        mRxManager.add(disposable);
    }

    @Override
    public void getCaptcha(String mobile, String type,String ne) {
        String captchaValidate = "";
        if (CaptchaUtil.CAPTCHA_NUMBER_TIMES >= Config.CAPTCHA_MAX_TIMES){
            Map<String,String> map = new HashMap<>();
            map.put("failureNum","3");
            map.put("clientTime",System.currentTimeMillis()/1000+"");
            captchaValidate = EncryptUtil.encryptRSA(new Gson().toJson(map),Config.captchaKey);
        }
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("mobile",mobile);
            jsonObject.put("type",type);
            jsonObject.put("NECaptchaValidate",ne);
            jsonObject.put("captchaValidate",captchaValidate);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),
                jsonObject.toString());
        Observable<CaptchaBean> observable = UserHttpService.getInstance().getCaptcha(body);
        Disposable disposable = toSubscribe(observable, new Consumer<CaptchaBean>() {
            @Override
            public void accept(@NonNull CaptchaBean bean) throws Exception {
                if("0".equals(bean.getCode())){
                    mView.getCaptchaSuccess();
                }else {
                    mView.getCaptchaFailed(bean.getMsg());
                }
            }
        },new ThrowableConsumer());
        mRxManager.add(disposable);
    }

    @Override
    public void pwdLogin(String mobile, String pwd) {
        Observable<LoginBean> observable = RetrofitUtil.getInstance().pwdLogin(mobile,pwd);
        Disposable disposable = toSubscribe(observable, new Consumer<LoginBean>() {
            @Override
            public void accept(@NonNull LoginBean bean) throws Exception {
                if(bean!=null &&"0".equals(bean.getCode())){
                    mView.loginSuccess(bean.getData());
                }else {
                    mView.loginFailed();
                }
            }
        }, new ThrowableConsumer());
        mRxManager.add(disposable);
    }
}